package com.oceansoft.core.common.util;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

/**
 * 文件处理
 * 
 * @author Administrator
 * 
 */
public final class FileUtil {
	public static void writeFile(byte[] content, String fileName) throws IOException {
		File t_Dir = new File(FilenameUtils.getFullPath(fileName));

		if ((!(t_Dir.exists())) && (!(t_Dir.mkdirs()))) {
			throw new IOException("The parent path is not valid.");
		}

		FileUtils.writeByteArrayToFile(new File(fileName), content);
	}

	public static void writeFile(String string, String filename) throws Exception {
		FileUtils.writeStringToFile(new File(filename), string);
	}

	public static void writeFile(String string, String filename, String charSet) throws Exception {
		FileUtils.writeStringToFile(new File(filename), string, charSet);
	}

	public static String classResourceToString(ClassLoader classLoader, String resourcePath, String charSet)
			throws IOException {
		return IOUtils.toString(classLoader.getResourceAsStream(resourcePath), charSet);
	}

	public static String readFile(String filename) throws Exception {
		return readFile(filename, OsUtil.getDefaultEncoding());
	}

	public static String readFile(String filename, String charSet) throws Exception {
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(filename);
			String str = IOUtils.toString(inputStream, charSet);

			return str;
		} finally {
			IOUtils.closeQuietly(inputStream);
		}
	}

	public static void copyDirectory(File sourceDirectory, File targetDirectory, FileFilter filter) throws IOException {
		if ((null == sourceDirectory) || (!(sourceDirectory.exists()))) {
			return;
		}

		if ((!(targetDirectory.exists())) && (!(targetDirectory.mkdirs()))) {
			throw new IOException("DirCopyFailed");
		}

		File[] t_Files = sourceDirectory.listFiles();

		for (int i = 0; i < t_Files.length; ++i) {
			File t_File = t_Files[i];
			if (t_File.isDirectory()) {
				File t_NextDirectory = new File(targetDirectory, t_File.getName());
				copyDirectory(t_File, t_NextDirectory, filter);
			} else if ((filter == null) || (filter.accept(t_Files[i]))) {
				copyFile(t_File, targetDirectory);
			}
		}
	}

	public static void copyFile(File sourceFile, File targetFile) throws IOException {
		if (targetFile.isDirectory()) {
			targetFile = new File(targetFile, sourceFile.getName());
		}

		FileInputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			inputStream = new FileInputStream(sourceFile);
			outputStream = new FileOutputStream(targetFile);
			IOUtils.copy(inputStream, outputStream);
		} finally {
			IOUtils.closeQuietly(inputStream);
			IOUtils.closeQuietly(outputStream);
		}
	}

	public static void deleteFile(File file) throws IOException {
		if (!(file.exists()))
			return;
		if (file.isFile()) {
			file.delete();
		} else
			FileUtils.deleteDirectory(file);
	}

	public static boolean mkdir(String fileName) {
		return mkdir(new File(fileName));
	}

	public static boolean mkdir(File file) {
		if (file.exists()) {
			return true;
		}
		if (!(file.isDirectory())) {
			file = file.getParentFile();
		}
		return file.mkdirs();
	}

	// public static String[] listFiles(String targetDir, String[]
	// includePatterns, String[] excludePatterns)
	// {
	// try
	// {
	// DirectoryScanner scan = new DirectoryScanner();
	// scan.setBasedir(targetDir);
	// scan.setIncludes(includePatterns);
	// scan.setExcludes(excludePatterns);
	// scan.scan();
	// return scan.getIncludedFiles(); } catch (Exception e) {
	// }
	// return new String[0];
	// }
}
